
package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.SupplierDao;
import org.lq.ht.entity.Supplier;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;
/**
 * 供应商数据访问层接口实现
 *@author 张冲
 * @qq2865346830
 * @package org.lq.ht.dao.impl
 * @date 2020年8月21日下午8:04:36
 */
public class SupplierDaoImpl implements SupplierDao , Instantiation<Supplier> {

	@Override
	public int save(Supplier t) {
		String sql ="insert into supplier (name,phone,address,company_name,card_no,note) values(?,?,?,?,?,?) ";
		int num=0;
		try {
			num=JDBCUtil.executeUpdate(sql, t.getName(),t.getPhone(),t.getAddress(),t.getCompany_name(),t.getCard_no(),t.getNote());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int update(Supplier t) {
		String sql ="update supplier set name=?,phone=?,address=?,company_name=?,card_no=?,note=? where no=?";
		int num =0;
		try {
			num =JDBCUtil.executeUpdate(sql,  t.getName(),t.getPhone(),t.getAddress(),t.getCompany_name(),t.getCard_no(),t.getNote(),t.getNo());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(int id) {
		String sql ="delete from supplier where no =?";
		int num = 0;
		try {
			num = JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public List<Supplier> findAll() {
		List<Supplier> list = new ArrayList<Supplier>();
		String sql = "select * from  supplier ";
		try {
			list=JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Supplier getById(int id) {
		List<Supplier> list = new ArrayList<Supplier>();
		String sql = "select * from  supplier  where no =?";
		try {
			list=JDBCUtil.executeQuery(sql, this,id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list.get(0);
	}

	@Override
	public Supplier instance(ResultSet rs) {
		Supplier s = new Supplier();
		try {
			s.setNo(rs.getInt("no"));
			s.setName(rs.getString("name"));
			s.setPhone(rs.getString("phone"));
			s.setAddress(rs.getString("address"));
			s.setCompany_name(rs.getString("company_name"));
			s.setCard_no(rs.getInt("card_no"));
			s.setNote(rs.getString("note"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return s;
	}
	



}


